package pl.edu.wsinf.lukaszwalczak.emailclient.controller;

import java.util.List;
import javafx.application.Platform;
import javafx.stage.Stage;
import javax.inject.Inject;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Account;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Attachment;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Folder;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Mail;
import pl.edu.wsinf.lukaszwalczak.emailclient.repository.AccountRepository;
import pl.edu.wsinf.lukaszwalczak.emailclient.repository.FolderRepository;
import pl.edu.wsinf.lukaszwalczak.emailclient.repository.MailRepository;
import pl.edu.wsinf.lukaszwalczak.emailclient.service.MailService;
import pl.edu.wsinf.lukaszwalczak.emailclient.view.MainWindowView;

/*
 * To jest klasa będąca kontrolerem we wzorcu MVC i odpowiada za odpowiadanie
 * na ruchy uzytkownika. W tym wypadku na klikniecie przez niego na przycisk,
 * ktory to wywoluje metode seyHello kontrolera. Ta metoda natomiast,
 * majac referencje do widoku uruchamia na nim metode ktora pokazuje tekst
 * Hello World at [jakas data]
 * Adnotacja @Component informuje Springa że ma zarządzać tą klasą.
 */
@Component
public class MainWindowController implements InitializingBean {

    @Inject
    private MainWindowView view;
    @Inject
    private AccountCreateController accountCreateController;
    @Inject
    private MailCreateController mailCreateController;
    @Inject
    private ContactListController contactListController;
    @Inject
    private FindController findController;
    private Stage primaryStage;
    @Inject
    private AccountRepository accountRepository;
    @Inject
    private MailRepository mailRepository;
    @Inject
    private FolderRepository folderRepository;
    @Inject
    private MailService mailService;
    @Inject
    private TaskController taskController;
    @Inject
    private TaskExecutor taskExecutor;
    @Inject
    private MailListController mailListController;

    public void showEmailAccountCreateWindow() {
        accountCreateController.showWindow();
    }

    public void showMailCreateWindow(Mail mail) {
        mailCreateController.showWindow(mail);
    }

    public void showMailReplyWindow(Mail mail) {
        mailCreateController.showWindow(mail);
        mailCreateController.setReplyValues(mail, false);
    }

    public void showMailReplyAllWindow(Mail mail) {
        mailCreateController.showWindow(mail);
        mailCreateController.setReplyValues(mail, true);
    }

    public void showMailForwardWindow(Mail mail) {
        mailCreateController.showWindow(mail);
        mailCreateController.setForwardValues(mail);
    }

    public void showContactListWindow() {
        contactListController.showWindow();
    }

    public void showFindWindow() {
        findController.showWindow();
    }

    public void showTaskWindow() {
        taskController.showWindow();
    }

    public void exit() {
        primaryStage.close();
    }

    public void downloadMails() {
        for (final Account account : accountRepository.findAll()) {
            taskExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    mailService.download(account);
                    //Uzywamy tego zeby metoda initMainWindowViewLeftPanel
                    //byla wywolana w watku JavaFX
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            initMainWindowViewLeftPanel();
                            reloadMailList();
                        }
                    });
                }
            });
        }
    }

    public List<Mail> getMails() {
        Sort sort = new Sort(Sort.Direction.DESC, "sentDate");
        return mailRepository.findAll(sort);
    }

    public List<Mail> findMailsByFolder(String folder) {
        Sort sort = new Sort(Sort.Direction.DESC, "sentDate");
        return mailRepository.findByFolder(folder, sort);
    }

    public List<Account> getAccounts() {
        return accountRepository.findAll();
    }

    public List<Folder> getFolders(Account account) {
        return folderRepository.findByAccount(account);
    }

    public void initMainWindowViewLeftPanel() {
        view.initLeft();
    }

    public void reloadMailList() {
        view.reloadMailList();
    }

    public void onMailSelection(Mail selected) {
        if (!selected.isRead()) {
            mailListController.setRead(selected, true);
        }
    }

    public void removeMail(Mail mail) {
        if (mail != null) {
            mailListController.removeMail(mail);
        }
    }

    public void moveToTrash(Mail mail) {
        if (mail != null) {
            mailListController.moveToTrash(mail);
        }
    }

    public void moveToFolder(Mail mail, String folder) {
        if (mail != null && folder != null) {
            mailListController.moveToFolder(mail, folder);
        }
    }

    public void handleAttachmentOpen(Attachment attachment, boolean forceSave) {
        mailService.handleAttachmentOpen(attachment, forceSave);
    }

    public void onPrimaryStageShow() {
        view.updateMailList();
    }

    public MainWindowView getView() {
        return view;
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public void setPrimaryStage(Stage primaryStage) {
        this.primaryStage = primaryStage;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        view.init();
    }
}
